// Package resp
// Author liuzhiyong
// Date 2023/11/27
// Description
package resp

import "time"

type Result[T interface{}] struct {
	Code      int    `json:"code"`
	Success   bool   `json:"success"`
	Message   string `json:"message"`
	Timestamp int64  `json:"timestamp"`
	Data      T      `json:"data"`
}

func NewSuccessResult[T interface{}](t T) *Result[T] {
	return &Result[T]{
		Code:      200,
		Success:   true,
		Message:   "操作成功！",
		Timestamp: time.Now().UnixMilli(),
		Data:      t,
	}
}

func NewFailResult() *Result[any] {
	return NewFailResultWithMsg("网络开小差了!")
}

func NewFailResultWithMsg(msg string) *Result[any] {
	return &Result[any]{
		Code:      500,
		Success:   false,
		Message:   msg,
		Timestamp: time.Now().UnixMilli(),
	}
}

func NewFailResultWithCode(code int, msg string) *Result[any] {
	return &Result[any]{
		Code:      code,
		Success:   false,
		Message:   msg,
		Timestamp: time.Now().UnixMilli(),
	}
}
